package com.situ.shoplook.admin.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.shoplook.admin.mapper.AdminMapper;
import com.situ.shoplook.admin.model.Admin;
import com.situ.shoplook.admin.service.AdminService;
import org.jasypt.util.password.PasswordEncryptor;
import org.jasypt.util.password.StrongPasswordEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author xinchen
 * @Date: 2024/08/30/ 9:42
 * @Description
 */
@Service
public class AdminServiceImpl implements AdminService {

    private AdminMapper adminMapper;

    @Autowired
    public void setAdminMapper(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }
    /**
     * add
     */
    @Override
    public int save(Admin admin) throws Exception {
        Admin admin1 = adminMapper.selectByLoginName(admin.getLoginName());
        if (!ObjectUtils.isEmpty(admin1)) {
            throw new Exception("用户名已存在");
        }
        if (ObjectUtils.isEmpty(admin.getLoginName())) {
            PasswordEncryptor pe  = new StrongPasswordEncryptor();
            String pass = pe.encryptPassword(admin.getLoginPassword());
            admin.setLoginPassword(pass);
        }
        //设置开启状态
        admin.setLocked(0);
        return this.adminMapper.insertSelective(admin);
    }

    /**
     * 更新
     * @throws Exception
     */
    @Override
    public int update(Admin admin) throws Exception {
        if (StringUtils.hasText(admin.getLoginName())){
            Admin admins = adminMapper.selectByLoginName(admin.getLoginName());
            if ( ! ObjectUtils.isEmpty(admins) && !admin.getAdminId().equals(admins.getAdminId())) {
                throw new Exception("用户名已存在");
            }
        }
        // 检查密码是否更改
        Admin originalAdmin = adminMapper.selectByPrimaryKey(Long.valueOf(admin.getAdminId()));
        if (originalAdmin != null && !originalAdmin.getLoginPassword().equals(admin.getLoginPassword())) {
            // 密码已更改，进行加密
            PasswordEncryptor pe = new StrongPasswordEncryptor();
            String encryptedPassword = pe.encryptPassword(admin.getLoginPassword());
            admin.setLoginPassword(encryptedPassword);
        }
        return adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public int remove(Integer[] ids) {
        int count =  adminMapper.deleteByIds(ids);
        System.out.println(count);
        return count;
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Admin admin)  {
        try(Page<?> __ = PageHelper.startPage(page, limit)) {
            List<Admin> list = adminMapper.search(admin);
            PageInfo pageInfo = new PageInfo<>(list);
            return pageInfo;
        }
    }
}
